package com.example.shaidemo;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HotelListFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HotelListFragment extends Fragment implements AdapterView.OnItemClickListener {

    private SimpleAdapter listItemAdapter; // 适配器
    ListView listView;
    SimpleAdapter simpleAdapter;
    private static final String TAG = "HotelListFragment";

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public HotelListFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HotelListFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HotelListFragment newInstance(String param1, String param2) {
        HotelListFragment fragment = new HotelListFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view  = inflater.inflate(R.layout.fragment_hotel_list, container, false);

        //进入筛选菜单
        TextView menuView = (TextView)view.findViewById(R.id.hotel_choose) ;
        menuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(getActivity(),HotelMenuActivity.class);
                startActivity(intent);
            }
        });

        listView = (ListView)view.findViewById(R.id.hotel_list);
        List<Map<String,String>> list = GetData.HotelList();
        simpleAdapter = new SimpleAdapter(getActivity(),list,
                R.layout.hotel_list_view,
                new String[]{"one","two","three","four","five","six","pic_1","seven","eight","pic_2"},
                new int[]{R.id.hotel_name,R.id.hotel_type,R.id.hotel_holiday,R.id.price_list,R.id.hotel_reliability,
                R.id.hotel_duration,R.id.hot_pic_1,R.id.place_label,R.id.place_detail,R.id.hot_pic_2});
        listView.setAdapter(simpleAdapter);
        //设置事件响应
        listView.setOnItemClickListener(this);

        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //从ListView中获取选中数据
        Intent intent = new Intent(getActivity(),HotelContentActivity.class);
        startActivity(intent);
    }
}